package mbu;

import java.awt.Color;
import robocode.BulletHitBulletEvent;
import robocode.HitByBulletEvent;
import robocode.HitRobotEvent;
import robocode.HitWallEvent;
import robocode.Robot;
import robocode.ScannedRobotEvent;
import robocode.WinEvent;

/**
 * Competitive Robot. Moves in a staircase pattern. Implements bullet power relative to the distance
 * from the enemy robot.
 * 
 * @author Michael Uranaka
 * @version 1.2
 * 
 */
public class StairShooter extends Robot {

  // Keep track of how many times the robot got hit.
  int hitByBulletCount = 0;

  /**
   * Run method is called by the game on start.
   */
  public void run() {

    // Set the robot color
    setColors(Color.BLACK, Color.WHITE, Color.BLUE, Color.MAGENTA, Color.GREEN);

    // Variable to alternate the turning direction.
    int turnDirection = 0;

    // Continue to loop for the remainder of the game.
    while (true) {

      // Turn right on even numbers.
      // Turn left on odd numbers.
      if ((turnDirection % 2) == 0) {

        // Use the gun to scan for robots.
        turnGunRight(360.0);

        // Perform a stair movement.
        turnRight(90);
        ahead(100);
        turnDirection++;

      }
      else if ((turnDirection % 2) != 0) {

        // Use the gun to scan for robots.
        turnGunLeft(360.0);

        // Perform a stair movement.
        turnLeft(90);
        ahead(100);
        turnDirection++;

      }
    }
  }

  /**
   * onScannedRobot is called by the game when a robot is scanned.
   * 
   * @param event ScannedRobotEvent.
   */
  public void onScannedRobot(ScannedRobotEvent event) {

    // Bullet power is relative to the distance from the enemy.
    // Code taken from sample robot Boom03.
    if (event.getDistance() >= 600.0 && event.getDistance() < 900.0) {
      fire(0.5);
    }
    else if (event.getDistance() >= 400.0 && event.getDistance() < 600.0) {
      fire(1);
    }
    else if (event.getDistance() >= 200.0 && event.getDistance() < 400.0) {
      fire(2);
    }
    else if (event.getDistance() >= 100.0 && event.getDistance() < 200.0) {
      fire(2.5);
    }
    else if (event.getDistance() >= 0.0 && event.getDistance() < 100.0) {
      fire(3); // Maximum power
    }

  }

  /**
   * onHitRobot is called by the game when a robot is hit.
   * 
   * @param event HitRobotEvent.
   */
  public void onHitRobot(HitRobotEvent event) {

    // Face the gun to the enemy and fire 3 bullets.
    turnGunRight(event.getBearing());
    fire(3);
    fire(3);
    fire(3);

  }

  /**
   * onHitWallEvent is called by the game when the robot hits the wall.
   * 
   * @param event HitWallEvent
   */
  public void onHitWall(HitWallEvent event) {

    // Move 200 pixels to the right.
    turnRight(90);
    ahead(200);

  }

  /**
   * onBulletHitBullet is called by the game when the robots bullet collides with an enemy bullet.
   * 
   * @param event BulletHitBulletEvent
   */
  public void onBulletHitBullet(BulletHitBulletEvent event) {

    // Move at a 45 degree angle.
    // 100 pixels to the left.
    turnLeft(45);
    ahead(100);

  }

  /**
   * onHitByBullet is called by the game when the robot is hit by an enemy bullet.
   * 
   * @param event HitByBulletEvent
   */
  public void onHitByBullet(HitByBulletEvent event) {

    // If the bullet count is even, move back.
    // Else the bullet count is odd so move forward.
    if (hitByBulletCount % 2 == 0) {
      back(200);
      hitByBulletCount++;
    }
    else {
      ahead(200);
      hitByBulletCount++;
    }

  }

  /**
   * onWin is called by the game when the robot wins.
   * 
   * @param event WinEvent
   */
  public void onWin(WinEvent event) {

    // Spin around twice.
    turnRight(720);

  }

}
